package com.study.bigwork.homework.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.study.bigwork.entity.Homework;

import com.study.bigwork.entity.User;

@Repository
public class HomeworkDao {
	@Resource
	private SessionFactory sessionFactory;
	public List<Homework> getHomeworks(String tag,String homeworkType){
		System.out.println("接受到的tag和homeworkType分别是"+tag+"  "+homeworkType);
		Session session=sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Homework where tag=? and homeworkType=?");
		query.setParameter(0,tag);
		query.setParameter(1,homeworkType);
		List<Homework> homeworks=query.getResultList();
		for(int i=0;i<homeworks.size();i++) {
			Homework homework=homeworks.get(i);
			homework.setResult_image();
			homework.setResult_image_teacher();
			homework.setHomework_image();
		}
		System.out.println("homeworks的值为："+homeworks);
		if(homeworks!=null) {
			return homeworks;
		}
		return null;
	}
	//查找作业
		public List<Homework> getHomeworks(int id){
			Session session=sessionFactory.getCurrentSession();
			Query query = session.createQuery("from Homework where user_id=?");
			query.setParameter(0,id);
			List<Homework> homeworks=query.getResultList();
			for(int i=0;i<homeworks.size();i++) {
				Homework homework=homeworks.get(i);
				homework.setResult_image();
				homework.setResult_image_teacher();
				homework.setHomework_image();
			}
			if(homeworks!=null) {
				return homeworks;
			}
			return null;
		}
		public List<Homework> getHomeworks(int teacher_id,String tag){
			Session session=sessionFactory.getCurrentSession();
			Query query = session.createQuery("from Homework where teacher_id=? and tag=?");
			query.setParameter(0,teacher_id);
			query.setParameter(1,tag);
			List<Homework> homeworks=query.getResultList();
			for(int i=0;i<homeworks.size();i++) {
				Homework homework=homeworks.get(i);
				homework.setResult_image();
				homework.setResult_image_teacher();
				homework.setHomework_image();
			}
			if(homeworks!=null) {
				return homeworks;
			}
			return null;
		}
	//添加作业信息
	public boolean addHomework(Homework homework) {
		Session session = this.sessionFactory.getCurrentSession();
		Gson gson = new Gson();
		homework.setTag("待批改");
		if(homework.getHomework_image()!=null) {
			String homework_image1=gson.toJson(homework.getHomework_image());
			homework.setHomework_image1(homework_image1);
		}
		if(homework.getResult_image()!=null) {
			String result_image1 = gson.toJson(homework.getResult_image());
			homework.setResult_image1(result_image1);
		}
		if(homework.getResult_image_teacher()!=null){
			String result_image_teacher1=gson.toJson(homework.getResult_image_teacher());
			homework.setResult_image_teacher1(result_image_teacher1);
		}
		
		
		session.save(homework);
		return true;
	}
	
	
	//修改作业
	public boolean updateHomework(Homework homework) {
		Session session = this.sessionFactory.getCurrentSession();
		//改成sql语句 
		String hql = "update Homework set grade=?,scored=? where id=?";
		Query query = session.createQuery(hql);
		query.setParameter(1, homework.getScored());
		query.setParameter(0, homework.getGrade());
		query.setParameter(2, homework.getId());
		int count = 0;
		count = query.executeUpdate();
		return count>0 ? true : false;
	}
	//根据tag值修改作业信息
	public boolean updateHomeworkInfo(int tag,Homework homework) {
		Session session=sessionFactory.getCurrentSession();
		if (tag == 1) {
			// 获取作业信息
			int id = homework.getId();
			List<String> result_images = homework.getResult_image();// 结果图片
			String result_image = "";
			Gson gson = new Gson();
			result_image = gson.toJson(result_images);

			// 拼接更新图片的sql语句
			String hql = "update Homework set result_image=? where id=?"; 
			
			Query query = session.createQuery(hql);
			query.setParameter(0, result_image);
			query.setParameter(1, id);
			int ret = query.executeUpdate();
			
			// 将订单的信息插入订单表中
			
			return ret > 0 ? true : false;

		} else if (tag == 2) {
			// 获取作业信息
			int id = homework.getId();

			List<String> result_images = homework.getResult_image();// 结果图片
			String result_image = "";
			Gson gson = new Gson();
			result_image = gson.toJson(result_images);

			List<String> result_image_teachers = homework.getResult_image_teacher();// 老师结果图片
			String result_image_teacher = "";
			Gson gson1 = new Gson();
			result_image_teacher = gson1.toJson(result_image_teachers);

			// 老师评语
			String result_text = homework.getResult_text();
			String hql = "update Homework set result_image=? ,result_image_teacher=?,result_text=?,tag=? where id=?"; 

			Query query = session.createQuery(hql);
			query.setParameter(0, result_image);
			query.setParameter(1,result_image_teacher);
			query.setParameter(2, result_text);
			query.setParameter(3, "批改完成");
			query.setParameter(4, id);
			int ret = query.executeUpdate();
		
			return ret > 0 ? true : false;

		}
		return false;
	}
	//更新作业的Tag标记
	public boolean updateHomeworkTag(int id, int teacher_id) {
		Session session=sessionFactory.getCurrentSession();
		// 获取订单信息
		String tag = "批改中";
		// 拼接更新蛋糕的sql语句
		String hql = "update Homework set teacher_id=? ,tag=? where id=?"; 

		Query query = session.createQuery(hql);
		query.setParameter(0,teacher_id);
		query.setParameter(1,tag);
		query.setParameter(2,id);
		int ret = query.executeUpdate();
		return ret > 0 ? true : false;
	}

}
